package com.atguigu.tingshu.account.service.impl;

import cn.hutool.core.util.IdUtil;
import com.atguigu.tingshu.account.mapper.RechargeInfoMapper;
import com.atguigu.tingshu.account.mapper.UserAccountDetailMapper;
import com.atguigu.tingshu.account.service.RechargeInfoService;
import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.account.RechargeInfo;
import com.atguigu.tingshu.model.account.UserAccount;
import com.atguigu.tingshu.model.account.UserAccountDetail;
import com.atguigu.tingshu.vo.account.RechargeInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Service
@SuppressWarnings({"all"})
public class RechargeInfoServiceImpl extends ServiceImpl<RechargeInfoMapper, RechargeInfo> implements RechargeInfoService {

    @Autowired
    private RechargeInfoMapper rechargeInfoMapper;


    /**
     * 根据充值订单编号查询充值信息
     *
     * @param orderNo
     * @return
     */
    @Override
    public RechargeInfo getRechargeInfo(String orderNo) {
        LambdaQueryWrapper<RechargeInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RechargeInfo::getOrderNo, orderNo);
        return rechargeInfoMapper.selectOne(queryWrapper);
    }

    /**
     * 保存充值记录
     *
     * @param rechargeInfoVo
     * @return {orderNo:"充值订单编号"} 对接微信支付接口
     */
    @Override
    public Map<String, String> submitRecharge(RechargeInfoVo rechargeInfoVo) {
        //1.封装充值记录并保存
        RechargeInfo rechargeInfo = new RechargeInfo();
        rechargeInfo.setUserId(AuthContextHolder.getUserId());
        //1.1 生成充值订单编号 形式：cz+雪花算法
        rechargeInfo.setOrderNo("cz" + IdUtil.getSnowflakeNextId());
        rechargeInfo.setRechargeStatus(SystemConstant.ORDER_STATUS_UNPAID);
        rechargeInfo.setRechargeAmount(rechargeInfoVo.getAmount());
        rechargeInfo.setPayWay(rechargeInfoVo.getPayWay());
        //1.2 保存充值记录
        rechargeInfoMapper.insert(rechargeInfo);

        //2.TODO 发送延迟关闭充值记录队列消息，进行延迟关单业务：参考以前订单模块的代码

        //3.封装订单编号
        Map<String, String> map = new HashMap<>();
        map.put("orderNo", rechargeInfo.getOrderNo());
        return map;
    }

    @Autowired
    private UserAccountService userAccountService;


    /**
     * 用户付款后处理充值成功业务
     *
     * @param orderNo
     */
    @Override
    public void rechargePaySuccess(String orderNo) {
        //1.修改充值状态
        //1.1. 根据充值订单编号查询充值记录
        LambdaQueryWrapper<RechargeInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RechargeInfo::getOrderNo, orderNo);
        RechargeInfo rechargeInfo = rechargeInfoMapper.selectOne(queryWrapper);

        if (SystemConstant.ORDER_STATUS_UNPAID.equals(rechargeInfo.getRechargeStatus())) {
            //1.2 修改充值状态：已支付
            rechargeInfo.setRechargeStatus(SystemConstant.ORDER_STATUS_PAID);
            rechargeInfoMapper.updateById(rechargeInfo);

            //2.余额充值
            LambdaUpdateWrapper<UserAccount> updateWrapper = new LambdaUpdateWrapper<>();
            BigDecimal amount = rechargeInfo.getRechargeAmount();
            updateWrapper.setSql("total_amount = total_amount + " + amount + ", available_amount = available_amount + " + amount + ", total_income_amount = total_income_amount + " + amount);
            updateWrapper.eq(UserAccount::getUserId, rechargeInfo.getUserId());
            userAccountService.update(updateWrapper);

            //3.新增账户变动日志
            userAccountService.saveUserAccountDetail(rechargeInfo.getUserId(), "充值：" + amount, SystemConstant.ACCOUNT_TRADE_TYPE_DEPOSIT, amount, orderNo);
        }
    }

    @Autowired
    private UserAccountDetailMapper userAccountDetailMapper;


    /**
     * 根据交易类型分页查询账户变动日志
     *
     * @param pageInfo
     * @param tradeType
     * @param userId
     * @return
     */
    @Override
    public void getUserAccountDetailPage(Page<UserAccountDetail> pageInfo, String tradeType, Long userId) {
        //1.创建分页查询条件对象
        LambdaQueryWrapper<UserAccountDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserAccountDetail::getUserId, userId);
        queryWrapper.eq(UserAccountDetail::getTradeType, tradeType);
        queryWrapper.select(UserAccountDetail::getOrderNo, UserAccountDetail::getTradeType, UserAccountDetail::getTitle, UserAccountDetail::getAmount);
        queryWrapper.orderByDesc(UserAccountDetail::getId);
        userAccountDetailMapper.selectPage(pageInfo, queryWrapper);
    }
}
